package com.novax.ex.activity.provider.controller.inner;


import com.novax.ex.activity.infrastructure.entity.TaskVersionConfig;
import com.novax.ex.activity.infrastructure.entity.TaskVersionInfo;
import com.novax.ex.activity.open.api.inner.TaskVersionInfoApi;

import com.novax.ex.activity.open.model.request.task.TaskVersionInfoRequest;
import com.novax.ex.activity.open.model.response.task.TaskDropDownResponse;
import com.novax.ex.activity.open.model.response.task.TaskVersionInfoResponse;
import com.novax.ex.activity.provider.controller.BaseActivityController;
import com.novax.ex.activity.provider.service.TaskTemplateService;

import com.novax.ex.activity.provider.service.TaskVersionConfigService;
import com.novax.ex.activity.provider.service.TaskVersionInfoService;
import com.novax.ex.common.constant.ActivityConstants;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.core.redis.RedisUtil;
import com.novax.ex.common.enums.TaskTemplateEnum;
import com.novax.ex.common.enums.TaskVersionConfigStatus;
import com.novax.ex.common.results.ReturnResult;

import com.novax.ex.common.util.CopyUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * @Description 后台-任务配置 controller
 * @Author ChenXi
 * @Date 2023/06/21/10:34
 */

@RestController
public class TaskVersionInfoController extends BaseActivityController implements TaskVersionInfoApi {

    @Resource
    private TaskTemplateService taskTemplateService;

    @Resource
    private TaskVersionConfigService taskVersionConfigService;

    @Resource
    private TaskVersionInfoService taskVersionInfoService;

    @Override
    public ReturnResult<List<TaskVersionInfoResponse>> query(String language, Long id) {
        if(id == null){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        HashMap<String, Object> query = new HashMap<>(1);
        query.put("versionId", id);
        List<TaskVersionInfo> list = taskVersionInfoService.findList(query);

        if(list.isEmpty()){
            return ReturnResult.success();
        }

        List<TaskVersionInfoResponse> responseList = CopyUtils.copyList(list, TaskVersionInfoResponse.class);
        return ReturnResult.success(getInternational(language, InternationalCode.QUERY_SUCCESS), responseList);
    }

    @Override
    public ReturnResult<List<TaskDropDownResponse>> dropDownBox() {
        return ReturnResult.success(CopyUtils.copyList(taskTemplateService.queryAll(), TaskDropDownResponse.class));
    }

    @Override
    public ReturnResult<?> taskModify(String language, Long id, TaskVersionInfoRequest taskVersionInfoRequest, BindingResult bindingResult) {
        // 缺少必填项
        if(bindingResult.hasErrors()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }

        TaskVersionInfo configTaskVersionInfo = taskVersionInfoService.find(id);
        if(Objects.isNull(configTaskVersionInfo)){
            return ReturnResult.fail(getInternational(language, InternationalCode.NOT_FOUND));
        }
        // 如果已上线 则不能对任务进行编辑
        TaskVersionConfig taskVersionConfig = taskVersionConfigService.find(configTaskVersionInfo.getVersionId());
        if(Objects.nonNull(taskVersionConfig) && taskVersionConfig.getState().equals(TaskVersionConfigStatus.online.getStatus())){
            return ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
        }

        Long taskId = taskVersionInfoRequest.getTaskId();
        // 任务为关注Twitter 和 加入telegram, 清空笔数
        if(taskId <= TaskTemplateEnum.JOIN_TELEGRAM.getTaskId()){
            taskVersionInfoRequest.setRechargeCount(null);
            taskVersionInfoRequest.setTransactionCount(null);
        }
        // 任务为充值
        if(taskId.equals(TaskTemplateEnum.COMPLETE_TOP_UP.getTaskId())){
            taskVersionInfoRequest.setTransactionCount(null);
        }
        // 任务为交易
        if(taskId > TaskTemplateEnum.COMPLETE_TOP_UP.getTaskId() && taskId <= TaskTemplateEnum.COMPLETE_ANY_TRANSACTION.getTaskId()){
            taskVersionInfoRequest.setRechargeCount(null);
        }

        boolean flag = taskVersionInfoService.updateTaskIdAndCount(
            id, taskId, taskVersionInfoRequest.getRechargeCount(), taskVersionInfoRequest.getTransactionCount());
        return flag ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS))
            : ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<?> verifyTwitter(Integer type) {
        if (Objects.equals(type, ActivityConstants.VERIFY.YES)) {
            RedisUtil.set(ActivityConstants.redisType.TWITTER_VERIFY, 1);
        } else {
            RedisUtil.delete(ActivityConstants.redisType.TWITTER_VERIFY);
        }
        return ReturnResult.success();
    }

    @Override
    public ReturnResult<?> verifyTwitterFind() {
        Object twitterKey = RedisUtil.get(ActivityConstants.redisType.TWITTER_VERIFY);
        return Objects.nonNull(twitterKey) ? ReturnResult.success(ActivityConstants.VERIFY.YES) : ReturnResult.success(ActivityConstants.VERIFY.NO);
    }

}
